//
//  SLZoomImageView.m
//  SLPhotoBrowser
//
//  Created by S_LINE on 2020/6/21.
//  Copyright © 2020 S_LINE. All rights reserved.
//

#import "SLZoomImageView.h"

#import "UIImageView+YYWebImage.h"
#import "SLCircleProgress.h"

@interface SLZoomImageView ()<UIScrollViewDelegate>

@property (nonatomic, assign) CGFloat defaultWidth;
@property (nonatomic, assign) CGFloat defaultHeight;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) SLCircleProgress *circleProgress;

@end

@implementation SLZoomImageView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.delegate = self;
        self.minimumZoomScale = 1.0;
        self.maximumZoomScale = 2.0;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.defaultWidth = frame.size.width;
        self.defaultHeight = frame.size.height;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.defaultWidth, self.defaultHeight)];
        self.imageView = imageView;
        [self addSubview:imageView];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.userInteractionEnabled = YES;
        imageView.clipsToBounds = YES;
        if (@available(iOS 11.0, *))
        {
            self.contentInsetAdjustmentBehavior =  UIScrollViewContentInsetAdjustmentNever;
        }
        CGFloat circleProgressWH = 30;
        CGFloat circleProgressX = self.defaultWidth / 2.0 - circleProgressWH / 2.0;
        CGFloat circleProgressY = self.defaultHeight / 2.0 - circleProgressWH / 2.0;
        CGRect circleProgressFrame = CGRectMake(circleProgressX, circleProgressY, circleProgressWH, circleProgressWH);
        SLCircleProgress *circleProgress = [[SLCircleProgress alloc] initWithFrame:circleProgressFrame];
        self.circleProgress = circleProgress;
        [self addSubview:circleProgress];
//        circleProgress.center = self.center;
    }
    return self;
}

- (void)pictureZoomWithScale:(CGFloat)zoomScale
{
    // 延中心点缩放
    CGFloat imageScaleWidth = zoomScale * self.defaultWidth;
    CGFloat imageScaleHeight = zoomScale * self.defaultHeight;
    CGFloat imageX = 0;
    CGFloat imageY = 0;
    if (imageScaleWidth < self.frame.size.width)
    {
        imageX = floorf((self.frame.size.width - imageScaleWidth) / 2.0);
    }
    if (imageScaleHeight < self.frame.size.height)
    {
        imageY = floorf((self.frame.size.height - imageScaleHeight) / 2.0);
    }
    self.imageView.frame = CGRectMake(imageX, imageY, imageScaleWidth, imageScaleHeight);
    self.contentSize = CGSizeMake(imageScaleWidth,imageScaleHeight);
}

- (void)recoverSize
{
    self.contentSize = CGSizeMake(self.defaultWidth, self.defaultHeight);
    self.zoomScale = 1;
    self.imageView.frame = CGRectMake(0, 0, self.defaultWidth, self.defaultHeight);
}

#pragma mark - UIScrollView代理方法
//返回需要缩放的视图控件 缩放过程中
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.imageView;
}

//开始缩放
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view{
}
//结束缩放
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
}

//缩放中
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    // 延中心点缩放
    CGFloat imageScaleWidth = scrollView.zoomScale * self.defaultWidth;
    CGFloat imageScaleHeight = scrollView.zoomScale * self.defaultHeight;
    CGFloat imageX = 0;
    CGFloat imageY = 0;
    if (imageScaleWidth < self.frame.size.width)
    {
        imageX = floorf((self.frame.size.width - imageScaleWidth) / 2.0);
    }
    if (imageScaleHeight < self.frame.size.height)
    {
        imageY = floorf((self.frame.size.height - imageScaleHeight) / 2.0);
    }
    self.imageView.frame = CGRectMake(imageX, imageY, imageScaleWidth, imageScaleHeight);
}

#pragma mark - setter
- (void)setImageUrl:(NSURL *)imageUrl
{
    _imageUrl = imageUrl;
    
    __weak typeof(self) weakSelf = self;
    [self.imageView yy_setImageWithURL:imageUrl placeholder:nil options:YYWebImageOptionSetImageWithFadeAnimation progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        CGFloat progress = (double)receivedSize / (double)expectedSize;
        weakSelf.circleProgress.progress = progress;
    } transform:^UIImage * _Nullable(UIImage * _Nonnull image, NSURL * _Nonnull url) {
        return image;
    } completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        weakSelf.circleProgress.hidden = YES;
    }];
}

- (void)setImage:(UIImage *)image {
    _image = image;
    self.imageView.image = image;
}

@end
